/**
 * 扁平化例子
 */
export const goodSymid = Symbol("goodid");
export interface Goods {
  [goodSymid]: number;
  name: string;
  price: number;
}

type Record<K extends keyof any, T> = {
  [P in K]: T;
};

// 相当于直接用id 就可以获取一条记录

// 返回的数据类型
type resultGoodsType = Record<number, Goods>;

// 模拟后台取出来的商品数据列表
const goodsList: Goods[] = [
  {
    [goodSymid]: 101,
    name: "苹果",
    price: 9,
  },
  {
    [goodSymid]: 102,
    name: "香蕉",
    price: 3,
  },
  {
    [goodSymid]: 103,
    name: "橘子",
    price: 3,
  },
];

// 把 goodsList扁平化成下面的对象格式
//{ '101': { name: '苹果', price: 9, [Symbol(goodid)]: 101 }
//'102': { name: '香蕉', price: 3, [Symbol(goodid)]: 101 },
// '103': { name: '橘子', price: 3, [Symbol(goodid)]: 101 },
// }

let goodRecord: Record<number,Goods> = {}

goodsList.forEach((goods)=>{
  goodRecord[goods[goodSymid]] = goods;
})

console.log('goodRecord',goodRecord);
console.log('goodRecord[101]',goodRecord[101]);


export {};
